package org.xteam.box2text.oaw;

import org.eclipse.xtend.expression.EvaluationException;
import org.eclipse.xtend.expression.ExecutionContext;
import org.eclipse.xtend.expression.Variable;
import org.eclipse.xtend.typesystem.Type;

public class BoxFacade {
	
	private BoxExecutionContext ctx;

	private BoxFacade(BoxExecutionContext ctxt) {
		this.ctx = ctxt;
	}
	
	public String evaluate(String definitionName, Object targetObject) {
		Type targetType = ctx.getType(targetObject);
		BoxRule def = ctx.findRule(definitionName, targetType);
		if (def == null)
			throw new EvaluationException("No rule for "
					+ targetType.getName() + " could be found in " + definitionName + " !", null, ctx);
		ctx = (BoxExecutionContext) ctx.cloneWithVariable(new Variable(ExecutionContext.IMPLICIT_VARIABLE,
				targetObject));
		ctx = (BoxExecutionContext) ctx.cloneWithResource(def.getOwner());
		return def.evaluate(ctx);
	}
	
	public static BoxFacade create(BoxExecutionContext ctxt) {
		return new BoxFacade(ctxt);
	}

}
